package com.wagn.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wagn.eduservice.entity.EduCourse;
import com.wagn.eduservice.entity.EduVideo;
import com.wagn.eduservice.entity.course.QueryCourse;
import com.wagn.eduservice.entity.vo.VoCourseInfo;
import com.wagn.eduservice.entity.vo.VoSetp3;
import com.wagn.eduservice.feign.VodFeign;
import com.wagn.eduservice.service.EduCourseService;
import com.wagn.eduservice.service.EduVideoService;
import com.wagn.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-17
 */
@RestController
@CrossOrigin
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private VodFeign vodFeign;

    @Autowired
    private EduVideoService eduVideoService;


    //添加课程基本信息
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody VoCourseInfo voCourseInfo){
        String id = eduCourseService.saveCourseInfo(voCourseInfo);
        return R.ok().data("courseId",id);
    }

    //回显step1中的数据，根据id查询chapter
    @GetMapping("getInfoById/{id}")
    public R getInfoById(@PathVariable String id){
        VoCourseInfo rs = eduCourseService.getInfoById(id);
        return R.ok().data("rows",rs);
    }

    //更新课程基本信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody VoCourseInfo voCourseInfo){
        String id = eduCourseService.updateCourseInfo(voCourseInfo);
        return R.ok().data("courseId",id);
    }

    //返回VoStep3
    @GetMapping("getVoStep3/{courseId}")
    public R getVoStep3(@PathVariable String courseId){
        VoSetp3 voSetp3 = eduCourseService.getVoStep3(courseId);
        return R.ok().data("rows",voSetp3);
    }

    //发布课程
    @PostMapping("publish/{id}")
    public R publish(@PathVariable String id){
        EduCourse eduCourse = eduCourseService.getById(id);
        Boolean rs=false;
        System.out.println("***"+rs+"***");
        if (eduCourse!=null){
            eduCourse.setStatus("Normal");
            rs = eduCourseService.updateById(eduCourse);
        }
        return R.ok().data("rows",rs);
    }

    //分页多条件查询
    @PostMapping("pageConditionCourse")
    public R pageConditionCourse(@RequestBody QueryCourse queryCourse){
        Page<EduCourse> page = eduCourseService.pageConditionCourse(queryCourse);
        long total = page.getTotal();
        List<EduCourse> rows = page.getRecords();
        return R.ok().data("total",total).data("rows",rows);
    }

    //删除课程
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        //远程调用,删除该课程的所有云视频
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        List<EduVideo> list = eduVideoService.list(wrapper);
        for(EduVideo e : list){
            vodFeign.remove(e.getVideoSourceId());
        }

        boolean rs = eduCourseService.delete(id);
        return R.ok().data("rows",rs);
    }

}

